
module iTimer.ui.Win ;
import iTimer.Types ;
import dwt.dwthelper.ByteArrayInputStream;
static const bool isClosedX	= true;

static class RepeatTimer : Runnable {
    private Display display ;
    private void function() fn ;
    private int timeout ;
    this( Display display , void function() fn , int timeout) {
        this.display    = display ;
        this.fn = fn ;
        this.timeout    = timeout;
        display.timerExec (timeout ,  this ) ;
    }
    void run(){
        auto now = Clock.now.unix ;
        fn();
        display.timerExec (timeout ,  this ) ;
    }
}

public class AppWin : Composite {
	static TrayItem     trayItem ;
	static Menu         trayMenu;
    static Image        appIcon ;
    static Label       bottom_msg_label ;
	
	static struct TrayMenuConf{
		const char[] 		name;
		const void function() evt;
		MenuItem 			item;
	}
	static const TrayMenuConf[]	TrayMenuConfs	= [
			{"退出", &Exit},
			TrayMenuConf.init,
			{"显示", &Show},
			{"隐藏", &Hide},			
			//TrayMenuConf.init,
			///{"配置", &Conf},
		];
	
	static void Main(){
		scope(exit){
			//
		}
		Display display = Display.getDefault();
		GShell = new Shell(display);
		
        static const appIconData = import(`iTimer.png`) ;
        appIcon = new Image (display, new ImageData(  new ByteArrayInputStream(cast(byte[]) appIconData) ) );
        // DWTResourceManager.getImage("BALL", GIFFILE)
        GShell.setImage(appIcon);
		GShell.setText( AppConf.title );
        AppMenus.load();
		createTrayMenu();
        
		auto inst = new AppWin(GShell, DWT.NULL);
		GShell.setLayout(new FillLayout());
		GShell.layout();
        inst.pack();
        /*
                Point size = inst.getSize();
		     GShell.setSize(shellBounds.width, shellBounds.height);
          */
        
		static void onPlayUpdate(){
               vPlayer.__update(false, false);
        }
        auto v1 = new RepeatTimer(display, &onPlayUpdate, AppConf.play_timeout );
            
            
		static void onTaskUpdate(){
            Task_List.update_task();
        }
        auto v2 = new RepeatTimer(display, &onTaskUpdate, AppConf.task_timeout );
  
		GShell.open();
		while (!GShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
    
	static void createTrayMenu(){

		void onMenuEvent(Event e){			
			char[] text	= (cast(MenuItem)e.widget).getText();
			foreach(ref i; TrayMenuConfs){
				if( i.name == text ){
					i.evt();
					break;
				}
			}
		}
		trayMenu = new Menu(GShell);
		for(auto i	= TrayMenuConfs.length;i--;){
			if( TrayMenuConfs[i] == TrayMenuConf.init ){
				new MenuItem(trayMenu, DWT.SEPARATOR);
			}else{
				TrayMenuConfs[i].item = new MenuItem(trayMenu, DWT.PUSH);
				TrayMenuConfs[i].item.setText(TrayMenuConfs[i].name);
				//TrayMenuConfs[i].item.handleEvent(GShell, DWT.Selection, &onMenuEvent);
				TrayMenuConfs[i].item.addListener (DWT.Selection, dgListener(&onMenuEvent));
			}
		}
		trayMenu.setDefaultItem(TrayMenuConfs[0].item);

		void onTrayEvent(Event e)
		{
			if(e.type == DWT.MenuDetect) {
				Point pt = GShell.getDisplay().getCursorLocation();
				if(GShell.getVisible){
					TrayMenuConfs[2].item.setEnabled(false);
					TrayMenuConfs[3].item.setEnabled(true);
				}else{
					TrayMenuConfs[2].item.setEnabled(true);
					TrayMenuConfs[3].item.setEnabled(false);
				}
				AppWin.trayMenu.setLocation(pt.x, pt.y);
				AppWin.trayMenu.setVisible(true);
			}
			if(e.type == DWT.DefaultSelection )	{
				if(GShell.getMinimized()) {
					GShell.setVisible(true);
					GShell.setMinimized(false);
				}else{
					GShell.setMinimized(true);
					GShell.setVisible(false);
				}
			}
		}
		GShell.addListener(DWT.Close, dgListener(delegate(Event e) {
			static if( !isClosedX ){
				GShell.setMinimized(true);
				GShell.setVisible(false);
				e.doit = false;
			}
		}) );
		GShell.addListener(DWT.Iconify, dgListener(delegate(Event e) {
			GShell.setMinimized(true);
			GShell.setVisible(false);
			e.doit = false;		
		}) );
		
		Tray tray	= GShell.getDisplay().getSystemTray();		
		trayItem	= new TrayItem(tray, DWT.NONE);
		trayItem.addListener( DWT.MenuDetect, dgListener(&onTrayEvent));
		trayItem.addListener(DWT.DefaultSelection, dgListener(&onTrayEvent));
		trayItem.setImage( appIcon );
		trayItem.setToolTipText( AppConf.title );
	}
	
	static void Exit(){
		// Exit All music 
		GShell.dispose();
	}
	static void Show(){
		GShell.setMinimized(false);
		GShell.setVisible(true);
		GShell.setActive();
	}
	static void Hide(){
		GShell.setVisible(false);
	}
	static void Conf(){
		//F7Conf.OpenLast();
	}

	
	this(Composite parent, int style) {
		super(parent, style);
		//DWTResourceManager.registerResourceUser(this);
		//GShell.setImage(DWTResourceManager.getImage("BALL", GIFFILE));
		// setSize(120, 180);

		auto thisLayout = new FormLayout(); 
        with(thisLayout){
            //  DWT. VERTICAL
            marginHeight = 0;
            marginWidth = 0 ;
            spacing =  0 ;
        }
		this.setLayout(thisLayout);
		
        
        Composite top_toolbar = new Composite(this, DWT.NONE);
        FormData data2 = new FormData();
        with( data2 ) {
            left    = new FormAttachment(0, 0);
            right   =  new FormAttachment(100, 0);
            top    = new FormAttachment(0, 0);
            height  = 26 ;
        }
        top_toolbar.setLayoutData(data2);
        AppToolsBar.load(top_toolbar);

        FormData data3 = new FormData();
        with( data3 ) {
            left    = new FormAttachment(0, 0);
            right   =  new FormAttachment(100, 0);
            top    = new FormAttachment(0, 28);
            bottom    = new FormAttachment(100, -28);
        }
        iTaskList.load(this, data3);
        
        FormData data4 = new FormData();
        with( data4 ) {
            left    = new FormAttachment(0, 4);
            right   =  new FormAttachment(100, 0);
            top    = new FormAttachment(100, - 20);
            bottom    = new FormAttachment(100, 0);
        }
        // Composite bottom_toolbar = new Composite(this, DWT.NONE);
        // bottom_toolbar.setLayoutData(data4);
        // bottom_toolbar.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_RED));
        
        bottom_msg_label = new Label(this, DWT.NONE );
        bottom_msg_label.setLayoutData(data4);
        bottom_msg_label.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_WIDGET_BACKGROUND));
        bottom_msg_label.setText("");
        
        
        /*
        
		Composite composite_Main = new Composite(this, DWT.NONE);
		composite_Main.setLayout(new FillLayout());
        
		GridLayout composite8Layout = new GridLayout();
		composite8Layout.marginWidth = 0;
		composite8Layout.marginHeight = 0;
		composite8Layout.numColumns = 3;
		composite8Layout.verticalSpacing = 0;
		composite8Layout.horizontalSpacing = 1;
		GridData composite8LData = new GridData();
		composite8LData.grabExcessHorizontalSpace = true;
		composite8LData.grabExcessVerticalSpace = true;
		composite8LData.verticalAlignment = GridData.FILL;
		composite8LData.horizontalAlignment = GridData.FILL;
		composite_Main.setLayout(composite8Layout);
		composite_Main.setLayoutData(composite8LData);
        
		composite_Main.layout();

        SashForm sashForm_Main = new SashForm(composite_Main, DWT.NONE);
        GridData sashForm2LData = new GridData();
        sashForm2LData.horizontalAlignment = GridData.FILL;
        sashForm2LData.grabExcessHorizontalSpace = true;
        sashForm2LData.verticalAlignment = GridData.FILL;
        sashForm2LData.grabExcessVerticalSpace = true;
        sashForm_Main.setOrientation(DWT.VERTICAL);
        sashForm_Main.setLayoutData(sashForm2LData);

        SashForm sashForm_top = new SashForm(sashForm_Main, DWT.NONE);
        sashForm_top.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_WHITE));


        //iEventList.load(sashForm_top);
        iTaskList.load(sashForm_top);
        //sashForm_top.setWeights([ 36, 64 ]);
        
        */
        
	}
}

